# -*- coding: utf-8 -*-
# created on 2017/1/16
# 

from mathsolver.functions.base import *
from mathsolver.functions.xiangliang.Vector_Fundamental import vector_xiangs
from sympy import Abs


def xl_get_all_child(expr, func):
    childrens = []
    if func(expr):
        return [expr]

    if hasattr(expr, "args"):
        for arg in expr.args:
            if func(arg):
                childrens.append(arg)
            else:
                childrens.extend(xl_get_all_child(arg, func))

        return childrens
    else:
        return childrens


def is_vector_abs(expr):
    answer = False
    if isinstance(expr, Abs):
        answer = True
    return answer


def is_vector2(expr):
    answer = False
    if not isinstance(expr, Add):
        if isinstance(expr, Pow):
            tt = expr.args[0]
            exp = expr.args[1]
            p = r'vector\(.*?\)'
            m = re.findall(p, str(tt))
            if len(m) == 1 and exp == 2:
                answer = True
    return answer


def is_vector_mul(expr):
    answer = False
    # if isinstance(expr, Mul):
    p = r'vector\(.{1,2}?\)[*]vector\(.{1,2}?\)'
    m = re.findall(p, str(expr))
    if m:
        answer = True
    return answer


def vector_get_muls(expr):
    p = r'vector\(.{1,3}?\)[*]vector\(.{1,3}?\)'
    m = re.findall(p, str(expr))
    m = set(m)
    m = list(m)
    muls = []
    if m:
        for item in m:
            muls.append(sympify(item))
    return muls


def vector_huajian(expr):
    new_expr = expr
    new_expr = new_expr.expand()
    abs_children = xl_get_all_child(new_expr, is_vector_abs)
    abs_children = set(abs_children)
    abs_children = list(abs_children)
    for Abs_Child in abs_children:
        abs_child_value = Abs_Child.args[0]
        if str(abs_child_value.func) == str("vector"):
            abs_child_value_name = abs_child_value.args[0]
            abs_child_value_value = str('Abs_') + str(abs_child_value_name)
            abs_child_value_value = sympify(abs_child_value_value)
            new_expr = new_expr.subs({Abs_Child: abs_child_value_value})
        else:
            new_expr = new_expr.subs({Abs_Child: sqrt(abs_child_value ** 2)})
    new_expr = new_expr.expand()
    ping_fangs = xl_get_all_child(new_expr, is_vector2)
    ping_fangs = set(ping_fangs)
    ping_fangs = list(ping_fangs)
    for PingFang in ping_fangs:
        ping_fang_base = PingFang.args[0]
        ping_fang_base_name = ping_fang_base.args[0]
        ping_fang_base_value = str('Abs_') + str(ping_fang_base_name)
        ping_fang_base_value = sympify(ping_fang_base_value)
        ping_fang_value = ping_fang_base_value * ping_fang_base_value
        new_expr = new_expr.subs({PingFang: ping_fang_value})
    muls = vector_get_muls(new_expr)
    muls = set(muls)
    muls = list(muls)
    for item in muls:
        new_item = item
        new_item_xiangs = vector_xiangs(new_item)
        new_item_angle = str('CAngle_')
        new_item_value = new_item
        for new_item_xiang in new_item_xiangs:
            new_xiang = new_item_xiang
            new_xiang_name = new_xiang.args[0]
            new_xiang_value = str('Abs_') + str(new_xiang_name)
            new_item_value = new_item_value.subs({new_xiang: new_xiang_value})
            new_item_angle += str(new_xiang_name)
        new_item_angle = sympify(new_item_angle)
        new_item_value = new_item_value * new_item_angle
        new_expr = new_expr.subs({new_item: new_item_value})
    return new_expr


if __name__ == '__main__':
    pass
